package com.dragon.modules.yuegou.service.impl.member;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.member.FavoriteEntity;
import com.dragon.modules.yuegou.enums.member.FavoriteType;
import com.dragon.modules.yuegou.mapper.member.FavoriteMapper;
import com.dragon.modules.yuegou.model.member.Favorite;
import com.dragon.modules.yuegou.model.member.Member;
import com.dragon.modules.yuegou.query.member.FavoriteQuery;
import com.dragon.modules.yuegou.service.member.FavoriteService;
import com.dragon.modules.yuegou.service.member.MemberService;
import com.dragon.modules.yuegou.utils.PageToResultUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 会员收藏 服务实现
 *
 * @author pbuilder generated
 * @date 2019-03-19 08:59:40
 * @version 2.0
 */
@Service("memFavoriteService")
public class FavoriteServiceImpl
		extends ServiceImpl<FavoriteMapper, FavoriteEntity>
		implements FavoriteService {

	@Autowired
	private MemberService memberService;

	@Override
	public Long totalTodayFansNum(String merchantId) {
		FavoriteQuery query = new FavoriteQuery();
		query.setObjectId(merchantId);
		query.setDel(false);
		query.createdTime().between(LocalDate.now().atStartOfDay(), LocalDateTime.now());
		return count(query.toQueryWrapper());
	}

	@Override
	public void collection(String merchantId, String busdId,String memberId) {
		Favorite favorite = new Favorite();
		favorite.setBusdId(busdId);
		favorite.setMemberId(memberId);
		favorite.setObjectId(merchantId);
		favorite.setType(FavoriteType.BUSINESS);
		baseMapper.insert(favorite);
	}

	@Override
	public void collectionByType(String busdId,String memberId, String objectId, Integer type) {
		//添加加入收藏时进行判断是否存在相同的收藏，存在不添加
		FavoriteQuery query = new FavoriteQuery();
		query.setBusdId(busdId);
		query.setMemberId(memberId);
		query.setObjectId(objectId);
		query.setDel(false);
		query.setType(FavoriteType.from(type));
		if (count(query.toQueryWrapper()) > 0) {
			return;
		}
		Favorite favorite = new Favorite();
		favorite.setBusdId(busdId);
		favorite.setMemberId(memberId);
		favorite.setObjectId(objectId);
		favorite.setType(FavoriteType.from(type));

		baseMapper.insert(favorite);
	}

	@Override
	public List<Member> merchantFans(PageRequest page, String merchantId) {
		FavoriteQuery query = new FavoriteQuery();
		query.setObjectId(merchantId);
		query.setType(FavoriteType.BUSINESS);
		query.setDel(false);
		IPage<Favorite> ipage = page(PageToResultUtils.resultToPage(page), query.toQueryWrapper());
		if (ipage.getTotal() > 0) {
			return ipage
					.getRecords()
					.stream()
					.map(f -> memberService.getMember(f.getMemberId()))
					.collect(Collectors.toList());
		}
		return null;
	}

	@Override
	public void followMerchant(String busdId, String merchantId,String memberId) {
		FavoriteQuery favoriteQuery = new FavoriteQuery();
		favoriteQuery.setMemberId(memberId);
		favoriteQuery.setObjectId(merchantId);
		favoriteQuery.setType(FavoriteType.BUSINESS);
		favoriteQuery.setDel(false);
		if (count(favoriteQuery.toQueryWrapper()) < 1) {
			Favorite favorite = new Favorite();
			favorite.setBusdId(busdId);
			favorite.setMemberId(memberId);
			favorite.setObjectId(merchantId);
			favorite.setType(FavoriteType.BUSINESS);
			favorite.setCreatedTime(LocalDateTime.now());
			favorite.setDel(false);
			baseMapper.insert(favorite);
		}
	}

	@Override
	public void cancelFollowMerchant(String busdId, String merchantId,String memberId) {
		FavoriteQuery favoriteQuery = new FavoriteQuery();
		favoriteQuery.setMemberId(memberId);
		favoriteQuery.setObjectId(merchantId);
		favoriteQuery.setBusdId(busdId);
		favoriteQuery.setType(FavoriteType.BUSINESS);
		remove(favoriteQuery.toQueryWrapper());
	}

	@Override
	public boolean isFavoriteByType(String busdId,String memberId, String objectId, FavoriteType type) {
		FavoriteQuery query = new FavoriteQuery();
		query.setMemberId(memberId);
		query.setObjectId(objectId);
		query.setType(type);
		query.setDel(false);
		Long count = count(query.toQueryWrapper());
		return count > 0;
	}

	@Override
	public boolean isFavoriteMerchant(String busdId,String memberId, String merchantId) {
		if (StringUtils.isBlank(merchantId)) return false;
		FavoriteQuery query = new FavoriteQuery();
		query.setMemberId(memberId);
		query.setObjectId(merchantId);
		query.setBusdId(busdId);
		Long count = count(query.toQueryWrapper());
		return count > 0;
	}

	@Override
	public boolean isFavoriteSpu( String memberId, String spuId) {
		if (StringUtils.isBlank(memberId)) return false;
		FavoriteQuery query = new FavoriteQuery();
		query.setMemberId(memberId);
		query.setObjectId(spuId);
		query.setDel(false);
		Long count = count(query.toQueryWrapper());
		return count > 0;
	}



    @Override
    public void cancelFollowSpu(String busdId, String memberId, String id) {
        FavoriteQuery query = new FavoriteQuery();
        if (StringUtils.isNotEmpty(busdId)) {
            query.setBusdId(busdId);
        }
        query.setObjectId(id);
        query.setMemberId(memberId);
        query.setDel(false);

		FavoriteEntity favorite = new FavoriteEntity();
		favorite.setDel(true);
		update(favorite,query.toQueryWrapper());
    }
}